package interfaces.hud.end;

import java.util.List;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;

import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.EndWindowText;
import fileHandling.language.properties.PlayerStats;
import fileHandling.language.properties.WeaponStats;

import logic.Stat;
import logic.StatsContainer;
import logic.common.game.Game;
import logic.common.player.Player;
import logic.common.player.awards.Award;
import logic.nodes.lod.BBNodeController;
import logic.ships.hunter.Hunter;
import logic.weapons.Weapon;
import interfaces.GUISource;
import interfaces.ImageUpdater;
import interfaces.hud.pilot.AwardView;
import interfaces.language.LanguageLabel;
import interfaces.superWidgets.HorizontalScrollContent;
import interfaces.superWidgets.InterfaceWindow;
import interfaces.superWidgets.KeyValueContent;
import interfaces.superWidgets.StaticContent;

public class PlayerStatsWindow extends InterfaceWindow {
	
	private static final String NONE_STRING = "-";
	
	private StaticContent hunterContent;
	private KeyValueContent playerContent, weaponsContent;
	private HorizontalScrollContent<AwardView> awardsContent;
	private Player player;
	private int border, labelHeight, topWidth;
	private ImageFont labelFont;
	
	public PlayerStatsWindow(Game game, int width, int height, int x, int y) {
		super(false, "");
		
		player = game.getCurrentPlayer();
		
		content.setLayoutManager(new StaticLayout());
		
		setSize(width, height);
		setXY(x, y);
		
		layout();

		border = content.getHeight() / 32;
		topWidth = (content.getWidth() - (4 * border)) / 3;
		labelFont = GUISource.middleFont;
		labelHeight = labelFont.getHeight();
				
		createHunterContent();
		createWeaponsContent();
		createPlayerContent();
		createAwardsContent();
		
		layout();
	}
	
	private void createHunterContent() {
		int size = topWidth;
		int x = border;
		int y = content.getHeight() - size - border;
		int imageSize = size - labelHeight;
		
		hunterContent = new StaticContent(size, size, x, y);
		content.addWidget(hunterContent);
		
		LanguageLabel lastHunterLabel = new LanguageLabel(EndWindowText.Last_Hunter, labelFont);
		GUISource.setTextColor(lastHunterLabel, Color.BLACK);
		lastHunterLabel.setXY(0, size - labelHeight);
		lastHunterLabel.setSize(size, labelHeight);
		hunterContent.addWidget(lastHunterLabel);
		
		Hunter h = player.getHunter();
		
		if(h == null) return;
		
		String f = h.getTeam().getFraction().toString().toLowerCase();
		String imagePath = "data/fractions/" + f + "/ships/" + h.getName() + "/" + BBNodeController.ICON_FILE;
		ImageUpdater hunterImage = new ImageUpdater(imagePath, 0, 0, imageSize, imageSize);
		hunterContent.addWidget(hunterImage);
	}
	
	private void createWeaponsContent() {
		int x = hunterContent.getWidth() + hunterContent.getX() + border;
		int y = hunterContent.getY();
		int size = topWidth;
		
		weaponsContent = new KeyValueContent(size, size, labelFont, true);
		weaponsContent.setXY(x, y);
		content.addWidget(weaponsContent);
		
		Hunter h = player.getHunter();
		
		int maxKills = 0;
		Weapon bestWeapon = null;
		
		if(h != null) {
			for(List<Weapon> list : h.getAllWeapons().values()) {
				for(Weapon w : list) {
					int k = w.getStats().getIntValue(WeaponStats.Kills);
					if(k > maxKills || bestWeapon == null) {
						bestWeapon = w;
						maxKills = k;
					}
				}
			}
		}
		
		String weaponName = NONE_STRING;
		if(bestWeapon != null) weaponName = bestWeapon.getName();
		
		weaponsContent.addLine(LanguageLoader.get(EndWindowText.Best_Weapon), weaponName);
		
		Stat[] keys = WeaponStats.values();
		StatsContainer stats = null;
		if(bestWeapon != null) stats = bestWeapon.getStats();
		setUpStatsContent(stats, keys, weaponsContent);
	}
	
	private void createPlayerContent() {
		int x = weaponsContent.getX() + weaponsContent.getWidth() + border;
		int y = weaponsContent.getY();
		int size = topWidth;
		
		playerContent = new KeyValueContent(size, size, labelFont, true);
		playerContent.setXY(x, y);
		content.addWidget(playerContent);
		
		Stat[] keys = PlayerStats.values();
		StatsContainer stats = player.getStats();
		setUpStatsContent(stats, keys, playerContent);
	}
	
	private void setUpStatsContent(StatsContainer stats, Stat[] keys, KeyValueContent statsContent) {
		for(Stat stat : keys) {
			String keyString = LanguageLoader.get(stat);
			
			String valueString = NONE_STRING;
			if(stats != null) valueString = stats.getValue(stat);

			statsContent.addLine(keyString, valueString);
		}
	}
	
	private void createAwardsContent() {
		int x = border;
		int y = border;
		int width = content.getWidth() - (2 * border);
		int height = hunterContent.getY() - (2 * border);
		
		awardsContent = new HorizontalScrollContent<AwardView>(width, height, x, y);
		addWidget(awardsContent);
		
		int elementSize = awardsContent.getElementWidth();
		
		for(Award award : player.getAwards()) {
			AwardView awardView = new AwardView(elementSize, elementSize, award);
			awardsContent.addElement(awardView);
		}
	}
}